01.webview_flutter 缓存

添加 Cache

WebViewController:

Future<void> _onAddToCache(BuildContext context) async {
  await webViewController.runJavaScript(
    'caches.open("test_caches_entry"); localStorage["test_localStorage"] = "dummy_entry";',
  );
  if (context.mounted) {
    ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
      content: Text('Added a test entry to cache.'),
    ));
  }
}

列出 Cache

WebViewController:

Future<void> _onListCache() {
  return webViewController.runJavaScript('caches.keys()'
      // ignore: missing_whitespace_between_adjacent_strings
      '.then((cacheKeys) => JSON.stringify({"cacheKeys" : cacheKeys, "localStorage" : localStorage}))'
      '.then((caches) => Toaster.postMessage(caches))');
}

清空 clearCache

WebViewController:

清空以下缓存:

  1. Browser HTTP Cache.
  2. Cache API caches. Service workers tend to use this cache.
  3. Application cache.
Future<void> clearCache() {

实例代码:

Future<void> _onClearCache(BuildContext context) async {
  await webViewController.clearCache();
  await webViewController.clearLocalStorage();
  if (context.mounted) {
    ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
      content: Text('Cache cleared.'),
    ));
  }
}

clearLocalStorage

WebViewController:

Future<void> clearLocalStorage() {

本文作者:Maeiee

本文链接:01.webview_flutter 缓存

版权声明:如无特别声明,本文即为原创文章,版权归 Maeiee 所有,未经允许不得转载!


喜欢我文章的朋友请随缘打赏,鼓励我创作更多更好的作品!